package dao;
import tool.OtherToolClass;
import tool.RowMapper;
import tool.BaseDao;
import java.util.List;
import java.sql.SQLException;
import java.sql.ResultSet;
import entity.MemorytbEntity;
import dao.mapper.MemorytbDAOI;

/**
 *@author JiangFan
 */
public class MemorytbDAO extends BaseDao implements MemorytbDAOI {

	OtherToolClass ot=new OtherToolClass ();
	/**
	 * @param color_name
	 *         根据内存名查询内存列表是否存在该内存
	 * @return
	 */
	@Override
	public MemorytbEntity selectMemoryOne (String color_name) throws SQLException, ClassNotFoundException {
		String sql="select *from memorytb where memory_name=?";
		Object[] param={color_name};
		//调用查询单个方法
		return this.excuteQueryOne (sql, param,new MemorytbRowMapper () );
	}

	/**
	 * @return
	 */
	@Override
	public List <MemorytbEntity> selectMemoryAll ()
			throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
		String sql="select *from memorytb";
		//调用查询方法
		return this.executeQuery (sql,null,new MemorytbRowMapper ());
	}

	/**
	 * @param me
	 *         在内存列表添加内存值
	 * @return
	 */
	@Override
	public int insertMemory (MemorytbEntity me) throws SQLException, ClassNotFoundException {
		String sql="insert into memorytb values(?,?)";
		me.setMemory_id (getID ());
		//调用添加方法
		Object[] param={me.getMemory_id (),me.getMemory_name ()};
		return this.update (sql,param);
	}

	private String getID () throws SQLException, ClassNotFoundException {
		String sql="select memory_id from memorytb";
		return ot.getIDtwo (sql,"mem",new MemorytbRowMapper ());
	}
}
class MemorytbRowMapper implements RowMapper<MemorytbEntity>{
	/**
	 * @param set
	 * @return
	 */
	@Override
	public MemorytbEntity rowmap(ResultSet set) throws  SQLException{
		return new MemorytbEntity(
				set.getString("memory_id"),
				set.getString("memory_name")
		);
	}
}
